package com.estate.struts.actions;

import java.util.Date;
import java.util.List;

import com.estate.business.TenderChangeService;
import com.estate.business.TenderService;
import com.estate.vo.Vo;
import com.estate.vo.impl.TenderChangeVo;
import com.estate.vo.impl.TenderVo;

public class TenderChangeAction extends EstateSupport {

	private String id;
	private String reason;
	private String handleResult;
	private String reamrk;
	private String tenderId;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private TenderChangeService tenderChangeService;
	private TenderService tenderService;
	
	private String directPage;
	
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());
		System.out.println(this.getId());
		String page = "";
		if("add".equals(this.getMethod())){
			TenderChangeVo tenderChange = this.getAddTenderChangeVo();
			if(tenderChange != null){
				boolean result = this.getTenderChangeService().addTenderChange(tenderChange);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("list".equals(this.getMethod())){
			List<Vo> list = this.getTenderChangeService().getTenderChangeList();
			this.getRequest().setAttribute("list", list);
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			page="allTenderChange";
		}else if("detail".equals(this.getMethod())){
			Vo vo = this.getTenderChangeService().getTenderChange(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderChange", vo);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo vo = this.getTenderChangeService().getTenderChange(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("tenderChange", vo);
			page = "update";
		}else if("update".equals(this.getMethod())){
			TenderChangeVo tenderChange = this.getUpdateTenderChange();
			if(tenderChange == null){
				page = "fail";
			}else{
				boolean result = this.getTenderChangeService().updateTenderChange(tenderChange);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}
		}
//		else if("post".equals(this.getMethod())){
//			this.getAnnouncementService().updatePost(Integer.parseInt(this.getId()));
//			page = this.SUCCESS;
//		}
		else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
//				System.out.println(this.getDelStr());
//				System.out.println(this.getDelStr().split(",").length);
				boolean result = this.getTenderChangeService().deleteMultiTenderChange(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else{
//				System.out.println("single delete");
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					TenderChangeVo tenderChange = new TenderChangeVo();
					tenderChange.setId(Integer.parseInt(this.getId()));
					boolean result = this.getTenderChangeService().deleteTenderChange(tenderChange);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else if("enterAddChange".equals(this.getMethod())){
			this.getRequest().setAttribute("tender", this.getTenderService().getTender(Integer.parseInt(this.getTenderId())));
			page = "enterAddChange";
		}else{
			page="fail";
		}
		
		if(this.getDirectPage() != null && !"".equals(this.getDirectPage())){
			page = this.getDirectPage();
		}
		return page;
	}
	private TenderChangeVo getUpdateTenderChange(){
		TenderChangeVo tenderChange = new TenderChangeVo();
		
		int id = Integer.parseInt(this.getId());
		
		tenderChange.setId(id);
		tenderChange.setHandleResult(this.getHandleResult());
		tenderChange.setReamrk(this.getReamrk());
		tenderChange.setReason(this.getReason());
		
		return tenderChange;
	}
	private TenderChangeVo getAddTenderChangeVo(){
		TenderChangeVo tenderChange = new TenderChangeVo();
		
		tenderChange.setHandleResult(this.getHandleResult());
		tenderChange.setPostDate(new Date());
		tenderChange.setPostUser(this.getSessionUser());
		tenderChange.setReamrk(this.getReamrk());
		tenderChange.setReason(this.getReason());
		
		TenderVo tender = new TenderVo();
		tender.setId(Integer.parseInt(this.getTenderId()));
		tenderChange.setTender(tender);
			
		return tenderChange;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getReason() {
		return reason;
	}
	public void setReason(String reason) {
		this.reason = reason;
	}
	public String getHandleResult() {
		return handleResult;
	}
	public void setHandleResult(String handleResult) {
		this.handleResult = handleResult;
	}
	public String getReamrk() {
		return reamrk;
	}
	public void setReamrk(String reamrk) {
		this.reamrk = reamrk;
	}
	public String getTenderId() {
		return tenderId;
	}
	public void setTenderId(String tenderId) {
		this.tenderId = tenderId;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getMultiType() {
		return multiType;
	}
	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public TenderChangeService getTenderChangeService() {
		return tenderChangeService;
	}
	public void setTenderChangeService(TenderChangeService tenderChangeService) {
		this.tenderChangeService = tenderChangeService;
	}
	public TenderService getTenderService() {
		return tenderService;
	}
	public void setTenderService(TenderService tenderService) {
		this.tenderService = tenderService;
	}
	public String getDirectPage() {
		return directPage;
	}
	public void setDirectPage(String directPage) {
		this.directPage = directPage;
	}
	
}
